import { DataTypes } from 'sequelize'

export default (sequelize) => {
  const Organization = sequelize.define('Organization', {
    name: {
      type: DataTypes.STRING,
      allowNull: false
    },
    code: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true
    },
    type: {
      type: DataTypes.ENUM('PARTY', 'ECONOMIC', 'SOCIAL'),
      allowNull: false
    },
    regionCode: {
      type: DataTypes.STRING,
      allowNull: false
    },
    parentId: DataTypes.INTEGER,
    level: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    description: DataTypes.STRING,
    leader: DataTypes.STRING
  })

  Organization.associate = (models) => {
    Organization.belongsTo(models.Region, {
      foreignKey: 'regionCode',
      targetKey: 'code'
    })
    Organization.belongsTo(Organization, {
      as: 'parent',
      foreignKey: 'parentId'
    })
    Organization.hasMany(Organization, {
      as: 'children',
      foreignKey: 'parentId'
    })
  }
  
  return Organization
} 